|
1
|
|
|
import { |
|
2
|
|
|
Controller, |
|
3
|
|
|
Inject, |
|
4
|
|
|
Get, |
|
5
|
|
|
UseGuards, |
|
6
|
|
|
Render, |
|
7
|
|
|
Query, |
|
8
|
|
|
Res, |
|
9
|
|
|
Param |
|
10
|
|
|
} from '@nestjs/common'; |
|
11
|
|
|
import { IQueryBus } from 'src/Application/IQueryBus'; |
|
12
|
|
|
import { GetUsersElementsQuery } from 'src/Application/HumanResource/Payslip/Query/GetUsersElementsQuery'; |
|
13
|
|
|
import { UserElementsView } from 'src/Application/HumanResource/Payslip/View/UserElementsView'; |
|
14
|
|
|
import { IsAuthenticatedGuard } from '../../User/Security/IsAuthenticatedGuard'; |
|
15
|
|
|
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; |
|
16
|
|
|
import { TableCsvFactory } from 'src/Infrastructure/Tables/TableCsvFactory'; |
|
17
|
|
|
import { PayrollElementsTableFactory } from '../Table/PayrollElementsTableFactory'; |
|
18
|
|
|
import { GetPayrollElementsControllerDTO } from '../DTO/GetPayrollElementsControllerDTO'; |
|
19
|
|
|
import { Response } from 'express'; |
|
20
|
|
|
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator'; |
|
21
|
|
|
import { formatHtmlYearMonth } from 'src/Infrastructure/Common/Utils/dateUtils'; |
|
22
|
|
|
|
|
23
|
|
|
@Controller('app/people/payroll_elements') |
|
24
|
|
|
@UseGuards(IsAuthenticatedGuard) |
|
25
|
|
|
export class GetPayrollElementsController { |
|
26
|
|
|
constructor( |
|
27
|
|
|
@Inject('IQueryBus') |
|
28
|
|
|
private readonly queryBus: IQueryBus, |
|
29
|
|
|
private readonly tableCsvFactory: TableCsvFactory, |
|
30
|
|
|
private readonly tableFactory: PayrollElementsTableFactory, |
|
31
|
|
|
@Inject('ITranslator') |
|
32
|
|
|
private readonly translator: ITranslator |
|
33
|
|
|
) {} |
|
34
|
|
|
|
|
35
|
|
|
@Get() |
|
36
|
|
|
@WithName('people_payroll_elements') |
|
37
|
|
|
@Render('pages/payroll_elements/index.njk') |
|
38
|
|
|
public async get(@Query() dto: GetPayrollElementsControllerDTO) { |
|
39
|
|
|
let date = new Date(); |
|
40
|
|
|
|
|
41
|
|
|
if (dto.year !== undefined && dto.month !== undefined) { |
|
42
|
|
|
date = new Date(dto.year, dto.month - 1, 15); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
const elements: UserElementsView[] = await this.queryBus.execute( |
|
46
|
|
|
new GetUsersElementsQuery(date) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
const table = this.tableFactory.create(elements); |
|
50
|
|
|
|
|
51
|
|
|
return { |
|
52
|
|
|
table, |
|
53
|
|
|
date, |
|
54
|
|
|
year: date.getUTCFullYear(), |
|
55
|
|
|
month: date.getUTCMonth() + 1 |
|
56
|
|
|
}; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
@Get('/:year-:month.csv') |
|
60
|
|
|
@WithName('people_payroll_elements_download') |
|
61
|
|
|
public async download( |
|
62
|
|
|
@Param() dto: GetPayrollElementsControllerDTO, |
|
63
|
|
|
@Res() res: Response |
|
64
|
|
|
) { |
|
65
|
|
|
const { table, date } = await this.get(dto); |
|
66
|
|
|
|
|
67
|
|
|
const csv = this.tableCsvFactory.toCsv(table); |
|
68
|
|
|
const filename = this.translator.translate('payroll-elements-filename', { |
|
69
|
|
|
date: formatHtmlYearMonth(date) |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
res.setHeader('Content-Disposition', `attachment; filename=${filename}`); |
|
73
|
|
|
res.setHeader('Content-Type', 'application/csv'); |
|
74
|
|
|
res.send(csv); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|